'
' DotNetNukeŽ - http://www.dotnetnuke.com
' Copyright (c) 2002-2006
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'
Imports System
Imports System.Web
Imports System.Web.Compilation
Imports System.Data
Imports System.Reflection
Imports System.Collections.Generic
Imports DotNetNuke

Namespace DotNetNuke.Modules.IWeb

    Public Class IWEBMethods

        Public Sub AddandDeleteMethods()
            ' Get Portals to remove
            Dim i As Integer = 0
            Dim arrPortalsOnServer As Array
            Dim PortalsWithoutMethods As New ArrayList()
            Dim lstAssemblies As List(Of IWEBMethodInfo)
            Dim objIWEBMethodInfo As IWEBMethodInfo
            Dim IWEBMethods As ArrayList
            Dim PortalsToRemove As String = ""
            Dim strDescription As String

            lstAssemblies = GetAssemblies()
            arrPortalsOnServer = PortalsOnServer()
 
            'Remove entries that no longer exist
            RemoveEntries(arrPortalsOnServer, lstAssemblies)

            'Add Methods

            ' * Loop through each portal
            For i = 0 To (arrPortalsOnServer.Length - 1) ' Loop through each Portal
                ' Get Methods for Portal
                IWEBMethods = GetMethodsInDatabase(CInt(arrPortalsOnServer(i)))

                For Each objIWEBMethodInfo In lstAssemblies ' Loop through each Assembly
                    ' * If the assembly is not in the portal add it
                    If IWEBMethods.IndexOf(objIWEBMethodInfo.MethodName) = -1 Then
                        Dim obj2IWEBMethodInfo As New IWEBMethodInfo
                        obj2IWEBMethodInfo.Portal = i
                        obj2IWEBMethodInfo.MethodName = Left(objIWEBMethodInfo.MethodName, 150)
                        obj2IWEBMethodInfo.MethodDescription = Left(objIWEBMethodInfo.MethodDescription, 1000)
                        obj2IWEBMethodInfo.SecuritySetting = "Host"
                        obj2IWEBMethodInfo.CompanyName = ExtractGroup(obj2IWEBMethodInfo.MethodDescription, "*")
                        obj2IWEBMethodInfo.ApplicationName = ExtractGroup(obj2IWEBMethodInfo.MethodDescription, "|")
                        obj2IWEBMethodInfo.FirstGrouping = ExtractGroup(obj2IWEBMethodInfo.MethodDescription, "#")
                        obj2IWEBMethodInfo.SecondGrouping = ExtractGroup(obj2IWEBMethodInfo.MethodDescription, "!")

                        If obj2IWEBMethodInfo.MethodDescription <> "" Then
                            strDescription = obj2IWEBMethodInfo.MethodDescription.Replace("*" & obj2IWEBMethodInfo.CompanyName & "*", "")
                            strDescription = strDescription.Replace("|" & obj2IWEBMethodInfo.ApplicationName & "|", "")
                            strDescription = strDescription.Replace("#" & obj2IWEBMethodInfo.FirstGrouping & "#", "")
                            strDescription = strDescription.Replace("!" & obj2IWEBMethodInfo.SecondGrouping & "!", "")
                            obj2IWEBMethodInfo.MethodDescription = strDescription
                        End If

                        AddMethod(obj2IWEBMethodInfo)
                    End If
                Next

            Next

        End Sub

#Region "RemovePortals"
        Public Sub RemoveEntries(ByVal PortalsOnServer As Array, ByVal colAssemblies As List(Of IWEBMethodInfo))
            Dim objIWEBMethodInfo As IWEBMethodInfo
            Dim strPortalsToRemove As String = ""
            Dim strMethodsToRemove As String = ""
            Dim strPortal As String = ""
            Dim strPortals As String = ""
            Dim strMethods As String = ""

            'Build Where clause
            For Each strPortal In PortalsOnServer
                strPortals &= strPortal & ","
            Next

            If strPortals <> "" Then
                'Strip off the last comma
                strPortals = strPortals.Substring(0, (strPortals.Length - 1))
            End If

            For Each objIWEBMethodInfo In colAssemblies
                strMethods &= "'" & objIWEBMethodInfo.MethodName.Replace("'", "") & "',"
            Next

            If strMethods <> "" Then
                'Strip off the last comma
                strMethods = strMethods.Substring(0, (strMethods.Length - 1))
            End If

            Dim mySqlString As New StringBuilder()
            mySqlString.Append("Delete ")
            mySqlString.Append("FROM {databaseOwner}{objectQualifier}IWebMethods ")
            mySqlString.Append("Where PortalID not in (" & strPortals & ") ")
            mySqlString.Append("Or MethodName not in (" & strMethods & ")")

            DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), Nothing)
        End Sub
#End Region

#Region "AddMethod"
        Public Sub AddMethod(ByVal objIWEBMethodInfo As IWEBMethodInfo)

            Dim mySqlString As New StringBuilder()
            mySqlString.Append("INSERT INTO {databaseOwner}{objectQualifier}IWebMethods ")
            mySqlString.Append("(PortalID, MethodName, MethodDescription, SecuritySetting, CompanyName, ApplicationName, FirstGrouping, SecondGrouping) ")
            mySqlString.Append("VALUES (@PortalID,@MethodName,@MethodDescription,@SecuritySetting,@CompanyName,@ApplicationName,@FirstGrouping,@SecondGrouping)")

            Dim prmPortalID As SqlParameter = New SqlParameter("@PortalID", SqlDbType.Int, 4)
            Dim prmMethodName As SqlParameter = New SqlParameter("@MethodName", SqlDbType.NVarChar, 150)
            Dim prmSecuritySetting As SqlParameter = New SqlParameter("@SecuritySetting", SqlDbType.NVarChar, 100)
            Dim prmCompanyName As SqlParameter = New SqlParameter("@CompanyName", SqlDbType.NVarChar, 100)
            Dim prmApplicationName As SqlParameter = New SqlParameter("@ApplicationName", SqlDbType.NVarChar, 100)
            Dim prmFirstGrouping As SqlParameter = New SqlParameter("@FirstGrouping", SqlDbType.NVarChar, 100)
            Dim prmSecondGrouping As SqlParameter = New SqlParameter("@SecondGrouping", SqlDbType.NVarChar, 100)
            Dim prmMethodDescription As SqlParameter = New SqlParameter("@MethodDescription", SqlDbType.NVarChar, 100)

            prmPortalID.Value = objIWEBMethodInfo.Portal
            prmMethodName.Value = objIWEBMethodInfo.MethodName
            prmSecuritySetting.Value = objIWEBMethodInfo.SecuritySetting
            prmCompanyName.Value = objIWEBMethodInfo.CompanyName
            prmApplicationName.Value = objIWEBMethodInfo.ApplicationName
            prmFirstGrouping.Value = objIWEBMethodInfo.FirstGrouping
            prmSecondGrouping.Value = objIWEBMethodInfo.SecondGrouping
            prmMethodDescription.Value = objIWEBMethodInfo.MethodDescription

            DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), prmPortalID, prmMethodName, prmMethodDescription, prmSecuritySetting, prmCompanyName, prmApplicationName, prmFirstGrouping, prmSecondGrouping)
        End Sub
#End Region

#Region "GetAssemblies"
        Public Shared Function GetAssemblies() As List(Of IWEBMethodInfo)
            Dim IWEBMethodInfoList As New List(Of IWEBMethodInfo)
            Dim IWEBMethodInfo As IWEBMethodInfo
            Dim WebMethodAttribute As System.Web.Services.WebMethodAttribute

            Dim objType As Type = Nothing
            Dim arrMethods As Array = Nothing
            objType = BuildManager.GetType("DotNetNuke.Modules.IWeb.WebServiceRoot", True, True)

            Dim arrMethodInfo() As MethodInfo
            Dim objMethodInfo As MethodInfo
            arrMethodInfo = objType.GetMethods

            For Each objMethodInfo In arrMethodInfo
                If ((objMethodInfo.IsHideBySig = False And objMethodInfo.IsSpecialName = False) Or (objMethodInfo.IsHideBySig = True And objMethodInfo.IsSpecialName = False)) Then
                    If (Not objMethodInfo.Module.Name.ToLower = "mscorlib.dll" And Not objMethodInfo.Module.Name.ToLower = "system.dll") Then
                        IWEBMethodInfo = New IWEBMethodInfo()
                        IWEBMethodInfo.MethodName = objMethodInfo.Name
                        If Attribute.IsDefined(objMethodInfo, GetType(System.Web.Services.WebMethodAttribute)) Then
                            WebMethodAttribute = Attribute.GetCustomAttribute(objMethodInfo, GetType(System.Web.Services.WebMethodAttribute))
                            IWEBMethodInfo.MethodDescription = WebMethodAttribute.Description
                        End If
                        IWEBMethodInfoList.Add(IWEBMethodInfo)
                    End If
                End If
            Next

            Return IWEBMethodInfoList

        End Function
#End Region

#Region "GetMethodsInDatabase"
        Public Shared Function GetMethodInDatabase(ByVal PortalID As Integer, ByVal MethodName As String) As IWEBMethodInfo
            Dim IWEBMethodInfo As New IWEBMethodInfo

            Dim mySqlString As New StringBuilder()
            mySqlString.Append("SELECT PortalID, MethodName, MethodDescription, SecuritySetting, CompanyName, ApplicationName, FirstGrouping, SecondGrouping ")
            mySqlString.Append("FROM {databaseOwner}{objectQualifier}IWebMethods ")
            mySqlString.Append("WHERE PortalID = '" & PortalID & "' ")
            mySqlString.Append("AND MethodName = '" & MethodName & "' ")

            Using dr As IDataReader = CType(DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), Nothing), IDataReader)
                While dr.Read
                    IWEBMethodInfo.MethodName = Convert.ToString(dr("MethodName"))
                    IWEBMethodInfo.Portal = Convert.ToString(dr("PortalID"))
                    IWEBMethodInfo.MethodDescription = Convert.ToString(dr("MethodDescription"))
                    IWEBMethodInfo.SecuritySetting = Convert.ToString(dr("SecuritySetting"))
                    IWEBMethodInfo.CompanyName = Convert.ToString(dr("CompanyName"))
                    IWEBMethodInfo.ApplicationName = Convert.ToString(dr("ApplicationName"))
                    IWEBMethodInfo.FirstGrouping = Convert.ToString(dr("FirstGrouping"))
                    IWEBMethodInfo.SecondGrouping = Convert.ToString(dr("SecondGrouping"))
                End While
            End Using

            Return IWEBMethodInfo
        End Function
        Public Shared Function GetMethodsInDatabase() As List(Of IWEBMethodInfo)
            Dim IWEBMethodInfoList As New List(Of IWEBMethodInfo)

            Dim mySqlString As New StringBuilder()
            mySqlString.Append("SELECT PortalID, MethodName, MethodDescription, SecuritySetting, CompanyName, ApplicationName, FirstGrouping, SecondGrouping ")
            mySqlString.Append("FROM {databaseOwner}{objectQualifier}IWebMethods ")

            mySqlString.Append("ORDER BY PortalID, MethodName ")

            Using dr As IDataReader = CType(DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), Nothing), IDataReader)
                While dr.Read
                    Dim IWEBMethodInfo As New IWEBMethodInfo()
                    IWEBMethodInfo.MethodName = Convert.ToString(dr("MethodName"))
                    IWEBMethodInfo.Portal = Convert.ToString(dr("PortalID"))
                    IWEBMethodInfo.MethodDescription = Convert.ToString(dr("MethodDescription"))
                    IWEBMethodInfo.SecuritySetting = Convert.ToString(dr("SecuritySetting"))
                    IWEBMethodInfo.CompanyName = Convert.ToString(dr("CompanyName"))
                    IWEBMethodInfo.ApplicationName = Convert.ToString(dr("ApplicationName"))
                    IWEBMethodInfo.FirstGrouping = Convert.ToString(dr("FirstGrouping"))
                    IWEBMethodInfo.SecondGrouping = Convert.ToString(dr("SecondGrouping"))
                    IWEBMethodInfoList.Add(IWEBMethodInfo)
                End While
            End Using
            Return IWEBMethodInfoList
        End Function
        Public Function GetMethodsInDatabase(ByVal Portal As Integer) As ArrayList
            Dim IWEBMethodInfoList As New ArrayList()

            Dim mySqlString As New StringBuilder()
            mySqlString.Append("SELECT MethodName ")
            mySqlString.Append("FROM {databaseOwner}{objectQualifier}IWebMethods ")
            mySqlString.Append("Where PortalID = " & Portal & " ")

            Using dr As IDataReader = CType(DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), Nothing), IDataReader)
                While dr.Read
                    IWEBMethodInfoList.Add(Convert.ToString(dr("MethodName")))
                End While
            End Using
            Return IWEBMethodInfoList
        End Function
#End Region

#Region "PortalsOnServer"
        Public Function PortalsOnServer() As Array

            Dim arrPortals As ArrayList
            Dim colPortalInfo As New PortalInfo()
            Dim colPortalController As New PortalController()
            arrPortals = colPortalController.GetPortals()
            Dim arrPortalIds(arrPortals.Count - 1) As Integer

            Dim i As Integer
            For i = 0 To (arrPortals.Count - 1)
                colPortalInfo = CType(arrPortals(i), PortalInfo)
                arrPortalIds(i) = colPortalInfo.PortalID
            Next i

            Return arrPortalIds
        End Function
#End Region

#Region "ExtractGroup"
        Private Function ExtractGroup(ByVal Description As String, ByVal Delimiter As String) As String

            '* = Company Name
            '|= Application Name
            '# = First Grouping
            '! = Second Grouping
            Dim strExtractGroup As String = ""
            Dim intFirstDelimiter As Integer = 0
            Dim intSecondDelimiter As Integer = 0

            ' No description was passed
            ' or it's length is too short to contain information
            If (Description = "") Or (Description.Length < 3) Then
                Return strExtractGroup
            End If

            'find first Delimiter
            intFirstDelimiter = Description.IndexOf(Delimiter)

            'Delimiter was not found 
            'or it is more than the second to the last character
            If (intFirstDelimiter = -1) _
            Or (intFirstDelimiter > Description.Length - 2) Then
                Return strExtractGroup
            End If

            intSecondDelimiter = Description.IndexOf(Delimiter, intFirstDelimiter + 1)

            'Delimiter was not found 
            If (intSecondDelimiter = -1) Then
                Return strExtractGroup
            End If

            'Extract data
            strExtractGroup = Description.Substring((intFirstDelimiter + 1), (intSecondDelimiter - (intFirstDelimiter + 1)))

            Return Left(strExtractGroup, 100)
        End Function
#End Region

#Region "UpdateSecuritySetting"
        Public Shared Sub UpdateSecuritySetting(ByVal PortalID As Integer, ByVal MethodName As String, ByVal SecuritySetting As String)

            Dim mySqlString As New StringBuilder()
            mySqlString.Append("Update {databaseOwner}{objectQualifier}IWebMethods ")
            mySqlString.Append("SET SecuritySetting = '" & SecuritySetting & "' ")
            mySqlString.Append("Where (PortalID = " & PortalID & ") ")
            mySqlString.Append("AND (MethodName = '" & MethodName & "') ")

            DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(mySqlString.ToString(), Nothing)
        End Sub
#End Region

    End Class
End Namespace